home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / pgen2 / parse.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.1 KB  |  206 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Parser engine for the grammar tables generated by pgen.
  5.  
  6. The grammar table must be loaded first.
  7.  
  8. See Parser/parser.c in the Python distribution for additional info on
  9. how this parsing engine works.
  10.  
  11. '''
  12. from  import token
  13.  
  14. class ParseError(Exception):
  15.     '''Exception to signal the parser is stuck.'''
  16.     
  17.     def __init__(self, msg, type, value, context):
  18.         Exception.__init__(self, '%s: type=%r, value=%r, context=%r' % (msg, type, value, context))
  19.         self.msg = msg
  20.         self.type = type
  21.         self.value = value
  22.         self.context = context
  23.  
  24.  
  25.  
  26. class Parser(object):
  27.     '''Parser engine.
  28.  
  29.     The proper usage sequence is:
  30.  
  31.     p = Parser(grammar, [converter])  # create instance
  32.     p.setup([start])                  # prepare for parsing
  33.     <for each input token>:
  34.         if p.addtoken(...):           # parse a token; may raise ParseError
  35.             break
  36.     root = p.rootnode                 # root of abstract syntax tree
  37.  
  38.     A Parser instance may be reused by calling setup() repeatedly.
  39.  
  40.     A Parser instance contains state pertaining to the current token
  41.     sequence, and should not be used concurrently by different threads
  42.     to parse separate token sequences.
  43.  
  44.     See driver.py for how to get input tokens by tokenizing a file or
  45.     string.
  46.  
  47.     Parsing is complete when addtoken() returns True; the root of the
  48.     abstract syntax tree can then be retrieved from the rootnode
  49.     instance variable.  When a syntax error occurs, addtoken() raises
  50.     the ParseError exception.  There is no error recovery; the parser
  51.     cannot be used after a syntax error was reported (but it can be
  52.     reinitialized by calling setup()).
  53.  
  54.     '''
  55.     
  56.     def __init__(self, grammar, convert = None):
  57.         '''Constructor.
  58.  
  59.         The grammar argument is a grammar.Grammar instance; see the
  60.         grammar module for more information.
  61.  
  62.         The parser is not ready yet for parsing; you must call the
  63.         setup() method to get it started.
  64.  
  65.         The optional convert argument is a function mapping concrete
  66.         syntax tree nodes to abstract syntax tree nodes.  If not
  67.         given, no conversion is done and the syntax tree produced is
  68.         the concrete syntax tree.  If given, it must be a function of
  69.         two arguments, the first being the grammar (a grammar.Grammar
  70.         instance), and the second being the concrete syntax tree node
  71.         to be converted.  The syntax tree is converted from the bottom
  72.         up.
  73.  
  74.         A concrete syntax tree node is a (type, value, context, nodes)
  75.         tuple, where type is the node type (a token or symbol number),
  76.         value is None for symbols and a string for tokens, context is
  77.         None or an opaque value used for error reporting (typically a
  78.         (lineno, offset) pair), and nodes is a list of children for
  79.         symbols, and None for tokens.
  80.  
  81.         An abstract syntax tree node may be anything; this is entirely
  82.         up to the converter function.
  83.  
  84.         '''
  85.         self.grammar = grammar
  86.         if not convert:
  87.             pass
  88.         
  89.         self.convert = lambda grammar, node: node
  90.  
  91.     
  92.     def setup(self, start = None):
  93.         """Prepare for parsing.
  94.  
  95.         This *must* be called before starting to parse.
  96.  
  97.         The optional argument is an alternative start symbol; it
  98.         defaults to the grammar's start symbol.
  99.  
  100.         You can use a Parser instance to parse any number of programs;
  101.         each time you call setup() the parser is reset to an initial
  102.         state determined by the (implicit or explicit) start symbol.
  103.  
  104.         """
  105.         if start is None:
  106.             start = self.grammar.start
  107.         
  108.         newnode = (start, None, None, [])
  109.         stackentry = (self.grammar.dfas[start], 0, newnode)
  110.         self.stack = [
  111.             stackentry]
  112.         self.rootnode = None
  113.         self.used_names = set()
  114.  
  115.     
  116.     def addtoken(self, type, value, context):
  117.         '''Add a token; return True iff this is the end of the program.'''
  118.         ilabel = self.classify(type, value, context)
  119.         while True:
  120.             (dfa, state, node) = self.stack[-1]
  121.             (states, first) = dfa
  122.             arcs = states[state]
  123.             for i, newstate in arcs:
  124.                 (t, v) = self.grammar.labels[i]
  125.                 if ilabel == i:
  126.                     if not t < 256:
  127.                         raise AssertionError
  128.                     self.shift(type, value, newstate, context)
  129.                     state = newstate
  130.                     while states[state] == [
  131.                         (0, state)]:
  132.                         self.pop()
  133.                         if not self.stack:
  134.                             return True
  135.                         (dfa, state, node) = self.stack[-1]
  136.                         (states, first) = dfa
  137.                         continue
  138.                         self.stack
  139.                     return False
  140.                 if t >= 256:
  141.                     itsdfa = self.grammar.dfas[t]
  142.                     (itsstates, itsfirst) = itsdfa
  143.                     if ilabel in itsfirst:
  144.                         self.push(t, self.grammar.dfas[t], newstate, context)
  145.                         break
  146.                     
  147.                 ilabel in itsfirst
  148.             elif (0, state) in arcs:
  149.                 self.pop()
  150.                 if not self.stack:
  151.                     raise ParseError('too much input', type, value, context)
  152.                 self.stack
  153.                 continue
  154.             ilabel == i
  155.             raise ParseError('bad input', type, value, context)
  156.  
  157.     
  158.     def classify(self, type, value, context):
  159.         '''Turn a token into a label.  (Internal)'''
  160.         if type == token.NAME:
  161.             self.used_names.add(value)
  162.             ilabel = self.grammar.keywords.get(value)
  163.             if ilabel is not None:
  164.                 return ilabel
  165.         
  166.         ilabel = self.grammar.tokens.get(type)
  167.         if ilabel is None:
  168.             raise ParseError('bad token', type, value, context)
  169.         ilabel is None
  170.         return ilabel
  171.  
  172.     
  173.     def shift(self, type, value, newstate, context):
  174.         '''Shift a token.  (Internal)'''
  175.         (dfa, state, node) = self.stack[-1]
  176.         newnode = (type, value, context, None)
  177.         newnode = self.convert(self.grammar, newnode)
  178.         if newnode is not None:
  179.             node[-1].append(newnode)
  180.         
  181.         self.stack[-1] = (dfa, newstate, node)
  182.  
  183.     
  184.     def push(self, type, newdfa, newstate, context):
  185.         '''Push a nonterminal.  (Internal)'''
  186.         (dfa, state, node) = self.stack[-1]
  187.         newnode = (type, None, context, [])
  188.         self.stack[-1] = (dfa, newstate, node)
  189.         self.stack.append((newdfa, 0, newnode))
  190.  
  191.     
  192.     def pop(self):
  193.         '''Pop a nonterminal.  (Internal)'''
  194.         (popdfa, popstate, popnode) = self.stack.pop()
  195.         newnode = self.convert(self.grammar, popnode)
  196.         if newnode is not None:
  197.             if self.stack:
  198.                 (dfa, state, node) = self.stack[-1]
  199.                 node[-1].append(newnode)
  200.             else:
  201.                 self.rootnode = newnode
  202.                 self.rootnode.used_names = self.used_names
  203.         
  204.  
  205.  
  206.